package com.laoxu.java.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.laoxu.java.model.Comment;
import com.laoxu.java.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class PostController {
    @Autowired
    CommentService commentService;

    @RequestMapping("/post")
    public String post(Model model){
        QueryWrapper query = new QueryWrapper();
        query.isNull("parent_id");

        List<Comment> list = commentService.list(query);
        // 第一层
        if(list.size() > 0 ){
            for (int i = 0; i < list.size(); i++) {
                // 递归查询下层
                getList(list.get(i));
            }
        }

        model.addAttribute("clist",list);

        return "post";
    }


    // 递归查询下级评论
    private void getList(Comment comment){
        QueryWrapper query = new QueryWrapper();
        query.eq("parent_id",comment.getId());

        List<Comment> comments = commentService.list(query);

        // 判断comments是否为空
        if(comments.size() > 0 ){
            comment.setCommentList(comments);
            for (int i = 0; i < comments.size(); i++) {
                // 递归查询下层
                getList(comments.get(i));
            }
        }

    }
}
